package com.hndzyu.yd.vip;

import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.gyf.barlibrary.ImmersionBar;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.UserBean;
import com.hndzyu.yd.utils.SharedPreferencesUtils;
import com.hndzyu.yd.widget.SignDialog;
import com.hndzyu.yd.widget.VipDIalog;

/**
 * Created by zz9527 on 2018/12/17.
 */
public class PDDActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {
    PDDFragment pddFragment;
    PDDSourceFragment pddSourceFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pdd);
        ImmersionBar.with(this).statusBarColor(R.color.them).fitsSystemWindows(true)
                .init();
        UserBean.divide = (String) SharedPreferencesUtils.getParam(this, "divide", "");
        UserBean.vipdivide = (String) SharedPreferencesUtils.getParam(this, "vipdivide", "");
        UserBean.uid = (int) SharedPreferencesUtils.getParam(this, "uid", 0);
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction beginTransaction = manager.beginTransaction();
        pddFragment = new PDDFragment();

        beginTransaction.add(R.id.fl, pddFragment);
        beginTransaction.commit();
        RadioGroup rg = findViewById(R.id.rg);
        rg.setOnCheckedChangeListener(this);
        rg.check(R.id.shangping);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        switch (i) {
            case R.id.shangping:
                if (UserBean.level > 0) {
                    if (pddSourceFragment == null) {
                        pddSourceFragment = new PDDSourceFragment();
                        fragmentTransaction.add(R.id.fl, pddSourceFragment);
                    }
                    fragmentTransaction.show(pddFragment);
                    fragmentTransaction.hide(pddSourceFragment);
                } else {
                    fragmentTransaction.show(pddFragment);
//                    fragmentTransaction.hide(pddSourceFragment);
                }
                break;
            case R.id.sucai:
                if (UserBean.level > 0) {
                    if (pddSourceFragment == null) {
                        pddSourceFragment = new PDDSourceFragment();
                        fragmentTransaction.add(R.id.fl, pddSourceFragment);
                    }
                    fragmentTransaction.hide(pddFragment);
                    fragmentTransaction.show(pddSourceFragment);
                } else {
                    final VipDIalog vipDIalog = new VipDIalog(this);
                    vipDIalog.setVIPListener(new VipDIalog.VIPListener() {
                        @Override
                        public void chooseTrue() {
                            final SignDialog dialog = new SignDialog(PDDActivity.this);
                            dialog.setListener(new SignDialog.SignListener() {
                                @Override
                                public void checktrue() {
                                    dialog.dismiss();
                                    ClipboardManager cm = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
                                    cm.setText(UserBean.kefu);
                                    Toast.makeText(PDDActivity.this, "客服号复制成功", Toast.LENGTH_SHORT).show();
                                }
                            });
                            dialog.show();
                            dialog.showYD();
                            dialog.showImg(R.mipmap.dialog_vip2);
                            vipDIalog.dismiss();
                        }

                        @Override
                        public void chooseFalse() {
                            vipDIalog.dismiss();

                        }
                    });
                    vipDIalog.show();
                    vipDIalog.setContent();
                }

                break;
        }
        fragmentTransaction.commit();
    }


}
